﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SmartSiteManagementSystem.AdminWeb.Extensions;
using SmartSiteManagementSystem.AdminWeb.Filter;
using SmartSiteManagementSystem.AdminWeb.Models;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.Models.User;
using SmartSiteManagementSystem.Models.DTO;
using System.Collections;
using SmartSiteManagementSystem.Models.Enums;
using SmartSiteManagementSystem.Utils;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{

  
    public class UserController : AdminBaseControllers
    {

        private readonly ILogger<UserController> _logger;

        //注入后台服务
        private readonly IAdminService _adminService;
        private readonly IUserService _userService;
        public UserController(IAdminService adminService, IUserService userService,  ILogger<UserController> logger) : base(adminService)
        {
            _logger = logger;
            _adminService = adminService;
            _userService = userService;
        }


        /// <summary>
        ///  物料首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public void GetList(string name, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            //if (catagoryId > 0)
            //    param.Add("catalog_id", catagoryId);
            int count = _userService.GetUserInfoListCount(param);
            IList<UserInfo> list = _userService.GetUserInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };

            HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(data));

        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="userId"></param>
        public void ajaxUpdateUserState(int userId)
        {
            UserInfo userInfo = null;
            bool success = false;
            if (userId > 0)
            {
                userInfo = _userService.GetUserInfoById(userId);
                userInfo.IsStop = userInfo.IsStop ? false : true;
                success = _userService.UpdateUserInfo(userInfo);

            }

            HttpContext.Response.WriteAsync(success.ToString());
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userId"></param>
        public void ajaxDeleteUser(int userId)
        {
            bool success = false;
            if (userId > 0)
            {
                UserInfo userInfo = _userService.GetUserInfoById(userId);
                if (userInfo != null)
                {
                    userInfo.IsDelete = true;
                    success = _userService.UpdateUserInfo(userInfo);
                }
            }
            HttpContext.Response.WriteAsync(success.ToString());
        }



        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ActionResult Access(int userId = 0)
        {
            UserInfo userInfo = new UserInfo();
            if (userId > 0)
            {
                userInfo = _userService.GetUserInfoById(userId);
            }
            ViewBag.userInfo = userInfo;

            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="LogonName"></param>
        /// <param name="PositionType"></param>
        /// <param name="ShowName"></param>
        /// <param name="Sex"></param>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        public void ajaxAccess(int UserId, string LogonName,int PositionType,string ShowName, int Sex, string Email, string Mobile)
        {
            UserInfo userInfo =  null;
            bool success = false;
            int code = -1;
            if (UserId > 0)
            {
                userInfo = _userService.GetUserInfoById(UserId);
            }
            else
            {



                userInfo = new UserInfo();
                userInfo.LogonName = LogonName;
                userInfo.LogonPwd = EncryptionUtils.GetUserLoginPwd_SHA1(LogonName, "666666");
                userInfo.IsStop = false;
                userInfo.CreateTime = DateTime.Now;
                //userInfo.LastestLogonIp =
                userInfo.RegType = (int)EUserRegLogonNameType.中台注册;
                userInfo.LastestLogonTime = DateTime.Now;
            }
            userInfo.IsDelete = false;
            userInfo.PositionType = PositionType;
            userInfo.Mobile = Mobile;
            userInfo.Email = Email;
            userInfo.Sex = (short)Sex;
            userInfo.ShowName = ShowName;

            if (UserId > 0)
            {
                success = _userService.UpdateUserInfo(userInfo);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                if (_userService.GetUserInfoByLogin(LogonName) == null)
                {

                    UserId = _userService.InsertUserInfo(userInfo);
                    success = UserId > 0 ? true : false;
                    if (success)
                    {
                        code = 1;
                    }
                }
                else
                {
                    code = -2;
                }
            }
            HttpContext.Response.WriteAsync(code.ToString());
        }


    }
}
